home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0059_256k Memory in BASM.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  3KB  |  101 lines

  1. {
  2.  CF> Ok I know in pascal you can basiclly us up t0 64k for varibles.... But how
  3.  CF> can I set it up to use... lets say 256k for varibles.  I mean, -XMS-
  4.  CF> memeory?                                 =- Chris Forbis
  5.  
  6. First, you may allocate 256k without XMS. Just using the following routines:
  7.  
  8.  
  9. function DosMaxAvail : longint;
  10. function MemAlloc(Size : longint) : pointer;
  11. function MemFree(P : pointer) : integer;
  12. function MemRealloc(P : pointer; NewSize : longint) : integer;
  13. }
  14.  
  15. Function DosMaxAvail : longint; assembler;
  16. { Returns the size of the largest contiguous free memory block
  17.   This function should be called ONLY when both HeapMin/HeapMax
  18.   memory allocation parameters set to zero }
  19. Asm
  20.   MOV BX,0FFFFh
  21.   MOV AH,48h
  22.   INT 21h
  23.   MOV AX,BX
  24.   MOV BX,16
  25.   MUL BX
  26. End; { DosMaxAvail }
  27.  
  28. Function MemAlloc(Size : longint) : pointer; assembler;
  29. { Creates a dynamic variable of the specified size and returns the pointer
  30.   to it. This function should be called ONLY when both HeapMin/HeapMax
  31.   memory allocation parameters set to zero }
  32. Asm
  33. @@1:
  34.   MOV AX,WORD PTR [Size]
  35.   MOV DX,WORD PTR [Size+2]
  36.   MOV CX,16
  37.   DIV CX
  38.   INC AX
  39.   MOV BX,AX
  40.   MOV AH,48h
  41.   INT 21h
  42.   JNC @@2
  43.   XOR AX,AX
  44. @@2:
  45.   MOV DX,AX
  46.   XOR AX,AX
  47. End; { MemAlloc }
  48.  
  49. Procedure MemFree(P : pointer); assembler;
  50. { Disposes of a given dynamic variable. This function should be called ONLY
  51.   when both HeapMin/HeapMax memory allocation parameters set to zero }
  52. Asm
  53.   MOV ES,WORD PTR [P+2]
  54.   MOV AH,49h
  55.   INT 21h
  56. End; { MemFree }
  57.  
  58. Function MemRealloc(P : pointer; NewSize : longint) : pointer; assembler;
  59. { Changes the size of en existed memory block. This function should be called
  60.   ONLY when both HeapMin/HeapMax memory allocation parameters set to zero }
  61. Asm
  62. @@1:
  63.   MOV AX,WORD PTR [NewSize]
  64.   PUSH AX
  65.   MOV DX,WORD PTR [NewSize+2]
  66.   PUSH DX
  67.   MOV CX,16
  68.   DIV CX
  69.   INC AX
  70.   MOV BX,AX
  71.   MOV AH,4Ah
  72.   INT 21h
  73.   POP DX
  74.   POP AX
  75.   JNC @@2
  76.   XOR DX,DX
  77.   XOR AX,AX
  78. @@2:
  79. End; { MemRealloc }
  80.  
  81. { Okey, the main program: }
  82.  
  83. {$M 4096,0,0}
  84.  
  85. const MemToAlloc = 256 * 1024; { 256k }
  86. var MemoryBlock : pointer;
  87. Begin
  88.   if DosMaxAvail >= MemToAlloc then
  89.   begin
  90.     WriteLn('Dos free memory before allocating ',
  91.       MemToAlloc shr 10, 'kb: ', DosMaxAvail shr 10, 'kb.');
  92.     MemoryBlock := MemAlloc(MemToAlloc);
  93.     WriteLn('Dos free memory after allocating ',
  94.       MemToAlloc shr 10, 'kb: ', DosMaxAvail shr 10, 'kb.');
  95.     { if MemoryBlock = nil then report an error... }
  96.     MemFree(MemoryBlock)
  97.   end else WriteLn('Not enough memory. ',
  98.     (MemToAlloc - DosMaxAvail) shr 10, 'kb more needed.')
  99. End.
  100.  
  101.